package com.yilin.tms.core.components.configuration;

import com.yilin.tms.core.components.interceptor.LoggerInterceptor;
import com.yilin.tms.core.components.interceptor.ParamInterceptor;
import com.yilin.tms.core.components.interceptor.ReqHttpInterceptor;
import com.yilin.tms.core.components.interceptor.TokenInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;


/**
 * 拦截器配置
 * */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {


    @Bean
    public LoggerInterceptor getLoggerInterceptor() {
        return new LoggerInterceptor();
    }

    public void addInterceptors(InterceptorRegistry registry) {
        //配置白名单
        List<String> patterns = new ArrayList<>();
        patterns.add("/xfiles/**");
        patterns.add("/error/**");

        InterceptorRegistration param = registry.addInterceptor(new ParamInterceptor());
        param.addPathPatterns("/**").excludePathPatterns(patterns);

        InterceptorRegistration abnormal = registry.addInterceptor(new ReqHttpInterceptor());
        abnormal.addPathPatterns("/**").excludePathPatterns(patterns);
        abnormal.excludePathPatterns("/user/login/generateCaptchaSlide");
        abnormal.excludePathPatterns("/user/login/validateCaptchaSlide");

        InterceptorRegistration token = registry.addInterceptor(new TokenInterceptor());
        token.addPathPatterns("/**").excludePathPatterns(patterns);
        token.excludePathPatterns("/data/action/upload/*");
        token.excludePathPatterns("/data/views/*");

        InterceptorRegistration logger = registry.addInterceptor(getLoggerInterceptor());
        logger.addPathPatterns("/**").excludePathPatterns(patterns);


    }
}
